// API服务文件
import { getAuthHeader } from './user.js'

// 基础配置
// const BASE_URL = 'http://39.91.138.170:8088' 
const BASE_URL = 'https://szhh.zaozhuangredcross.org.cn'
// const BASE_URL = 'http://redcross.api.zhongzhixinsd.com'

// 请求封装
const request = (options) => {
  return new Promise((resolve, reject) => {
    // 获取认证头部
    const authHeader = getAuthHeader()
    
    uni.request({
      url: BASE_URL + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: {
        'Content-Type': 'application/json',
        ...authHeader,
        ...options.header,
        'token': uni.getStorageSync('user_token'),
      },
      success: (res) => {
        if(res.data.code == 401){
          // 清除登录信息但不跳转
          try {
            uni.removeStorageSync('user_token')
            uni.removeStorageSync('user_info')
            uni.removeStorageSync('login_time')
          } catch (e) {
            console.error('清除登录信息失败:', e)
          }
          reject(res.data)
          return
        }
        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 志愿者申请提交
export const submitVolunteerApplication = (formData) => {
  return request({
    url: '/rcapi/member/joinVolunteer',
    method: 'POST',
    data: formData
  })
}

// 获取志愿者详情
export const getVolunteerInfo = () => {
  return request({
    url: '/rcapi/member/volunteerInfo',
    method: 'GET'
  })
}

// 获取民族列表
export const getNationList = () => {
  return request({
    url: '/api/common/nations',
    method: 'GET'
  })
}

// 获取政治面貌列表
export const getPoliticalList = () => {
  return request({
    url: '/api/common/political',
    method: 'GET'
  })
}

// 获取文化程度列表
export const getEducationList = () => {
  return request({
    url: '/api/common/education',
    method: 'GET'
  })
}

// 获取字典值
export const getDictValByKey = (key) => {
  return request({
    url: `/rcapi/cms/getDictValByKey?key=${key}`,
    method: 'GET'
  })
}

// 上传文件
export const uploadFile = (filePath) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: BASE_URL + '/oss/apiUpload',
      filePath: filePath,
      name: 'file',
      success: (res) => {
        const data = JSON.parse(res.data)
        resolve(data)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 用户登录
export const userLogin = (loginData) => {
  return request({
    url: '/rcapi/member/smsLogin',
    method: 'POST',
    data: loginData
  })
}

// 发送短信验证码
export const sendSmsCode = (phone, continueReg = false) => {
  return request({
    url: '/rcapi/member/sendLoginCode',
    method: 'POST',
    data: { phone, continueReg }
  })
}

// 快速注册
export const quickReg = (regData) => {
  return request({
    url: '/rcapi/user/quickReg',
    method: 'POST',
    data: regData
  })
}

// 微信登录
export const wxLogin = (loginData) => {
  return request({
    url: '/rcapi/user/wxLogin',
    method: 'POST',
    data: loginData
  })
}

// 微信获取手机号（登录后调用）
export const wxGetUserPhone = (code, openid) => {
  return request({
    url: '/rcapi/user/wxGetUserPhone',
    method: 'POST',
    data: { code, openid }
  })
}

// 获取CMS分组数据
export const getCmsGroupByCateId = (type) => {
  return request({
    url: '/rcapi/cms/groupByType',
    method: 'GET',
    data: { type }
  })
}

// 获取用户信息
export const getUserInfo = () => {
  return request({
    url: '/rcapi/member/memberInfo',
    method: 'GET'
  })
}

// 更新用户信息
export const updateUserInfo = (userData) => {
  return request({
    url: '/rcapi/member/update',
    method: 'PUT',
    data: userData
  })
}

// 编辑用户信息
export const editUserInfo = (userData) => {
  return request({
    url: '/rcapi/member/edit',
    method: 'POST',
    data: userData
  })
}

// 用户退出登录
export const userLogout = () => {
  return request({
    url: '/rcapi/member/logout',
    method: 'POST'
  })
}

// ========== 应急救援救援队相关接口 ==========

// 获取救援队列表
export const getRescueTeamList = (params = {}) => {
  return request({
    url: '/api/rescue-teams',
    method: 'GET',
    data: params
  })
}

// 获取救援队详情
export const getRescueTeamDetail = (id) => {
  return request({
    url: `/api/rescue-teams/${id}`,
    method: 'GET'
  })
}

// 创建救援队
export const createRescueTeam = (teamData) => {
  return request({
    url: '/api/rescue-teams',
    method: 'POST',
    data: teamData
  })
}

// 更新救援队信息
export const updateRescueTeam = (id, teamData) => {
  return request({
    url: `/api/rescue-teams/${id}`,
    method: 'PUT',
    data: teamData
  })
}

// 删除救援队
export const deleteRescueTeam = (id) => {
  return request({
    url: `/api/rescue-teams/${id}`,
    method: 'DELETE'
  })
}

// 获取救援类型列表
export const getRescueTypes = () => {
  return request({
    url: '/api/rescue-types',
    method: 'GET'
  })
}

// 获取区域列表
export const getAreas = () => {
  return request({
    url: '/api/areas',
    method: 'GET'
  })
}

// 获取救援队统计信息
export const getRescueTeamStats = () => {
  return request({
    url: '/api/rescue-teams/stats',
    method: 'GET'
  })
}

// 批量导入救援队数据
export const importRescueTeams = (fileData) => {
  return request({
    url: '/api/rescue-teams/import',
    method: 'POST',
    data: fileData
  })
}

// 导出救援队数据
export const exportRescueTeams = (params = {}) => {
  return request({
    url: '/api/rescue-teams/export',
    method: 'GET',
    data: params
  })
} 


// 获取献血地点列表
export const getBloodAddressList = () => {
  return request({
    url: '/rcapi/cms/pageContributeAddress',
    method: 'GET'
  })
}

// 捐献登记
export const cellCheckIn = (formData) => {
  return request({
    url: '/rcapi/cms/cellCheckIn',
    method: 'POST',
    data: formData
  })
}

// 献血登记
export const bloodCheckIn = (formData) => {
  return request({
    url: '/rcapi/cms/bloodCheckIn',
    method: 'POST',
    data: formData
  })
}

// 救助类型列表
export const getRescueTypeList = () => {
  return request({
    url: '/rcapi/cms/listHelpType',
    method: 'GET'
  })
}

// 救助登记
export const helpCheckIn = (formData) => {
  return request({
    url: '/rcapi/cms/helpCheckIn',
    method: 'POST',
    data: formData
  })
}

// 应急救护报名登记
export const firstAidCheckIn = (formData) => {
  return request({
    url: '/rcapi/cms/firstAidCheckIn',
    method: 'POST',
    data: formData
  })
}

// 爱心捐款
export const donate = (formData) => {
  return request({
    url: '/rcapi/cms/donate',
    method: 'get',
    data: formData
  })
}


//爱心捐物
export const donateMaterial = (formData) => {
  return request({
    url: '/rcapi/cms/bestowCheckIn',
    method: 'post',
    data: formData
  })
}


// 联系我们
export const contactUs = (formData) => {
  return request({
    url: '/rcapi/cms/contactUs',
    method: 'get',
    data: formData
  })
}

// 成为会员申请提交
export const joinRedCross = (formData) => {
  return request({
    url: '/rcapi/member/joinRedCross',
    method: 'POST',
    data: formData
  })
}

// 获取会员详情
export const getMemberInfo = () => {
  return request({
    url: '/rcapi/member/memberInfo',
    method: 'GET'
  })
}

// 获取会员卡片信息
export const getMemberCard = (memberId) => {
  return request({
    url: `/rcapi/member/getMemberCard/${memberId}`,
    method: 'GET'
  })
}

// 编辑会员卡片
export const editMemberCard = (data) => {
  return request({
    url: '/rcapi/member/editMemberCard',
    method: 'POST',
    data: data
  })
}

// 获取banner列表
export const getBannerList = () => {
  return request({
    url: '/rcapi/cms/listBanner',
    method: 'GET'
  })
}

// ========== 订单相关接口 ==========

// 创建订单
export const createOrder = (orderData) => {
  return request({
    url: '/rcapi/order/createOrder',
    method: 'POST',
    data: orderData
  })
}

// 支付接口
export const payOrder = (payData) => {
  return request({
    url: '/rcapi/order/pay',
    method: 'POST',
    data: payData
  })
}

// 获取订单详情
export const getOrderDetail = (orderId) => {
  return request({
    url: `/rcapi/order/detail/${orderId}`,
    method: 'GET'
  })
}

// 获取订单列表
export const getOrderList = (params = {}) => {
  return request({
    url: '/rcapi/order/orderList',
    method: 'GET',
    data: params
  })
}

// 申请开发票
export const applyInvoice = (invoiceData) => {
  return request({
    url: '/rcapi/order/applyInvoice',
    method: 'POST',
    data: invoiceData
  })
}

// 获取费用列表
export const getFeeList = () => {
  return request({
    url: '/rcapi/order/feeList',
    method: 'GET'
  })
}

// 获取入会机构列表
export const getUnitList = () => {
  return request({
    url: '/rcapi/member/unitList',
    method: 'GET'
  })
}

